{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#标准方程法_岭回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import genfromtxt\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  83.     234.289  235.6    159.     107.608 1947.      60.323]\n",
      " [  88.5    259.426  232.5    145.6    108.632 1948.      61.122]\n",
      " [  88.2    258.054  368.2    161.6    109.773 1949.      60.171]\n",
      " [  89.5    284.599  335.1    165.     110.929 1950.      61.187]\n",
      " [  96.2    328.975  209.9    309.9    112.075 1951.      63.221]\n",
      " [  98.1    346.999  193.2    359.4    113.27  1952.      63.639]\n",
      " [  99.     365.385  187.     354.7    115.094 1953.      64.989]\n",
      " [ 100.     363.112  357.8    335.     116.219 1954.      63.761]\n",
      " [ 101.2    397.469  290.4    304.8    117.388 1955.      66.019]\n",
      " [ 104.6    419.18   282.2    285.7    118.734 1956.      67.857]\n",
      " [ 108.4    442.769  293.6    279.8    120.445 1957.      68.169]\n",
      " [ 110.8    444.546  468.1    263.7    121.95  1958.      66.513]\n",
      " [ 112.6    482.704  381.3    255.2    123.366 1959.      68.655]\n",
      " [ 114.2    502.601  393.1    251.4    125.368 1960.      69.564]\n",
      " [ 115.7    518.173  480.6    257.2    127.852 1961.      69.331]\n",
      " [ 116.9    554.894  400.7    282.7    130.081 1962.      70.551]]\n"
     ]
    }
   ],
   "source": [
    "#读入数据\n",
    "data = genfromtxt(\"longley.csv\",delimiter = ',')\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 234.289  235.6    159.     107.608 1947.      60.323]\n",
      " [ 259.426  232.5    145.6    108.632 1948.      61.122]\n",
      " [ 258.054  368.2    161.6    109.773 1949.      60.171]\n",
      " [ 284.599  335.1    165.     110.929 1950.      61.187]\n",
      " [ 328.975  209.9    309.9    112.075 1951.      63.221]\n",
      " [ 346.999  193.2    359.4    113.27  1952.      63.639]\n",
      " [ 365.385  187.     354.7    115.094 1953.      64.989]\n",
      " [ 363.112  357.8    335.     116.219 1954.      63.761]\n",
      " [ 397.469  290.4    304.8    117.388 1955.      66.019]\n",
      " [ 419.18   282.2    285.7    118.734 1956.      67.857]\n",
      " [ 442.769  293.6    279.8    120.445 1957.      68.169]\n",
      " [ 444.546  468.1    263.7    121.95  1958.      66.513]\n",
      " [ 482.704  381.3    255.2    123.366 1959.      68.655]\n",
      " [ 502.601  393.1    251.4    125.368 1960.      69.564]\n",
      " [ 518.173  480.6    257.2    127.852 1961.      69.331]\n",
      " [ 554.894  400.7    282.7    130.081 1962.      70.551]]\n",
      "[[ 83. ]\n",
      " [ 88.5]\n",
      " [ 88.2]\n",
      " [ 89.5]\n",
      " [ 96.2]\n",
      " [ 98.1]\n",
      " [ 99. ]\n",
      " [100. ]\n",
      " [101.2]\n",
      " [104.6]\n",
      " [108.4]\n",
      " [110.8]\n",
      " [112.6]\n",
      " [114.2]\n",
      " [115.7]\n",
      " [116.9]]\n"
     ]
    }
   ],
   "source": [
    "#切分数据\n",
    "x_data = data[:,1:]\n",
    "y_data = data[:,0,np.newaxis]\n",
    "print(x_data)\n",
    "print(y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(16, 6)\n",
      "(16, 1)\n",
      "(16, 7)\n"
     ]
    }
   ],
   "source": [
    "print(np.mat(x_data).shape)\n",
    "print(np.mat(y_data).shape)\n",
    "#给样本添加偏置项\n",
    "X_data = np.concatenate((np.ones((16,1)),x_data),axis=1)\n",
    "print(X_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#标准方程法_岭回归求解回归参数\n",
    "def weights(xArr,yArr,lam=0.2):\n",
    "    #lam表示岭回归系数\n",
    "    xMat = np.mat(xArr)\n",
    "    yMat = np.mat(yArr)\n",
    "    xTx = xMat.T * xMat\n",
    "    #np.eye(xMat.shape[1])表示创建单位矩阵\n",
    "    rxTx = xTx + np.eye(xMat.shape[1]) * lam\n",
    "    #计算矩阵的值，如果为0，说明没有逆矩阵\n",
    "    if np.linalg.det(rxTx) == 0.0:\n",
    "        print(\"This matrix cannot do inverse\")\n",
    "        return\n",
    "    #xTx.I为xTx的逆矩阵\n",
    "    ws = rxTx.I * xMat.T * yMat\n",
    "    return ws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 7.38107538e-04]\n",
      " [ 2.07703836e-01]\n",
      " [ 2.10076376e-02]\n",
      " [ 5.05385441e-03]\n",
      " [-1.59173066e+00]\n",
      " [ 1.10442920e-01]\n",
      " [-2.42280461e-01]]\n"
     ]
    }
   ],
   "source": [
    "ws = weights(X_data,y_data)\n",
    "print(ws)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 83.55075226],\n",
       "        [ 86.92588689],\n",
       "        [ 88.09720228],\n",
       "        [ 90.95677622],\n",
       "        [ 96.06951002],\n",
       "        [ 97.81955375],\n",
       "        [ 98.36444357],\n",
       "        [ 99.99814266],\n",
       "        [103.26832266],\n",
       "        [105.03165135],\n",
       "        [107.45224671],\n",
       "        [109.52190685],\n",
       "        [112.91863666],\n",
       "        [113.98357055],\n",
       "        [115.29845063],\n",
       "        [117.64279933]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算预测值\n",
    "np.mat(X_data) * np.mat(ws)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sklearn_岭回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import genfromtxt\n",
    "from sklearn import linear_model\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  83.     234.289  235.6    159.     107.608 1947.      60.323]\n",
      " [  88.5    259.426  232.5    145.6    108.632 1948.      61.122]\n",
      " [  88.2    258.054  368.2    161.6    109.773 1949.      60.171]\n",
      " [  89.5    284.599  335.1    165.     110.929 1950.      61.187]\n",
      " [  96.2    328.975  209.9    309.9    112.075 1951.      63.221]\n",
      " [  98.1    346.999  193.2    359.4    113.27  1952.      63.639]\n",
      " [  99.     365.385  187.     354.7    115.094 1953.      64.989]\n",
      " [ 100.     363.112  357.8    335.     116.219 1954.      63.761]\n",
      " [ 101.2    397.469  290.4    304.8    117.388 1955.      66.019]\n",
      " [ 104.6    419.18   282.2    285.7    118.734 1956.      67.857]\n",
      " [ 108.4    442.769  293.6    279.8    120.445 1957.      68.169]\n",
      " [ 110.8    444.546  468.1    263.7    121.95  1958.      66.513]\n",
      " [ 112.6    482.704  381.3    255.2    123.366 1959.      68.655]\n",
      " [ 114.2    502.601  393.1    251.4    125.368 1960.      69.564]\n",
      " [ 115.7    518.173  480.6    257.2    127.852 1961.      69.331]\n",
      " [ 116.9    554.894  400.7    282.7    130.081 1962.      70.551]]\n"
     ]
    }
   ],
   "source": [
    "#读入数据\n",
    "data = genfromtxt(\"longley.csv\",delimiter = ',')\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 234.289  235.6    159.     107.608 1947.      60.323]\n",
      " [ 259.426  232.5    145.6    108.632 1948.      61.122]\n",
      " [ 258.054  368.2    161.6    109.773 1949.      60.171]\n",
      " [ 284.599  335.1    165.     110.929 1950.      61.187]\n",
      " [ 328.975  209.9    309.9    112.075 1951.      63.221]\n",
      " [ 346.999  193.2    359.4    113.27  1952.      63.639]\n",
      " [ 365.385  187.     354.7    115.094 1953.      64.989]\n",
      " [ 363.112  357.8    335.     116.219 1954.      63.761]\n",
      " [ 397.469  290.4    304.8    117.388 1955.      66.019]\n",
      " [ 419.18   282.2    285.7    118.734 1956.      67.857]\n",
      " [ 442.769  293.6    279.8    120.445 1957.      68.169]\n",
      " [ 444.546  468.1    263.7    121.95  1958.      66.513]\n",
      " [ 482.704  381.3    255.2    123.366 1959.      68.655]\n",
      " [ 502.601  393.1    251.4    125.368 1960.      69.564]\n",
      " [ 518.173  480.6    257.2    127.852 1961.      69.331]\n",
      " [ 554.894  400.7    282.7    130.081 1962.      70.551]]\n",
      "[ 83.   88.5  88.2  89.5  96.2  98.1  99.  100.  101.2 104.6 108.4 110.8\n",
      " 112.6 114.2 115.7 116.9]\n"
     ]
    }
   ],
   "source": [
    "#切分数据\n",
    "x_data = data[:,1:]\n",
    "y_data = data[:,0]\n",
    "print(x_data)\n",
    "print(y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4046363636363637\n",
      "(16, 100)\n"
     ]
    }
   ],
   "source": [
    "#创建模型\n",
    "#生成100个岭回归系数测试值\n",
    "alphas_to_test = np.linspace(0.001,1,100)\n",
    "#创建模型，保存误差值\n",
    "#Ridge代表岭回归\n",
    "#CV 代表交叉验证法\n",
    "model = linear_model.RidgeCV(alphas = alphas_to_test , store_cv_values=True)\n",
    "model.fit(x_data,y_data)\n",
    "\n",
    "#岭系数\n",
    "print(model.alpha_)\n",
    "#loss值\n",
    "print(model.cv_values_.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAD8CAYAAABw1c+bAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAFGNJREFUeJzt3X+M5PV93/Hni4VLlIBrl7vI1nHncySsxIqTYq3snJCSs2gsQJXxH1HliGBsOUZKQ+NLSVUXV00VN6VKVfJDckPOxYmpSEgbSHKKSCkirFLHC2KPUGM4UV2IbU5cxNm4gETL+bh3//jOhvHc7O53dmdndub7fEir+e58PzP3+dzuvuYz7+/n+51UFZKk7rhg2h2QJE2WwS9JHWPwS1LHGPyS1DEGvyR1jMEvSR1j8EtSxxj8ktQxBr8kdcyF0+7AMLt3764DBw5MuxuSNDOOHTv2jara06btjgz+AwcOsLKyMu1uSNLMSPK1tm0t9UhSxxj8ktQxBr8kdYzBL0kdY/BLUscY/JLUMXMV/MvLcNttza0kabgduY5/M5aX4aqr4MwZ2LULHnoIDh6cdq8kaeeZmxn/0lIT+q+/3twuLU27R5K0M81N8B861Mz0Fxaa20OHpt0jSdqZ5qbUc/BgU95ZWmpC3zKPJA03N8EPTdgb+JK0vrkp9UiS2tkw+JPsS/JwkuNJnkryySFtrk/y5d7Xl5L8SN++q5M8k+REkk+NewCSpNG0KfWcBW6pqseTXAIcS/JgVT3d1+ZvgB+vqm8luQY4ArwvyQLwWeAngJPAY0mODjxWkjRBG874q+pUVT3e234FOA7sHWjzpar6Vu/bR4DLetvvBU5U1b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8fdh1mc8b8XOFFVz1bVGeAe4LqBNtcBX+ht/yFwVbL6AXwzacMxV9XDVbX66SSPAJdNuI/j1ubnDPAZ4FeB/zfJzm2TNmP+BPDZqvoWQFW9MOE+jlubMRfwpt723wOen2D/xq6q/gJ4cZ0m1wF3VeMR4M1J3jbOPsxi8O8Fnuv7/mTvvqFtquos8BJw6UR6tz3ajLnfx2lmDLNswzEnuQLYV1V/OsmObaM2P+d3Au9M8pdJHkly9cR6tz3ajPnfAD+d5CRwP/BPJ9O1qRn1731ks/hBLMNm7oNLk9q0mSWtx5Pkp4FF4Me3tUfbb90xJ7kA+DXgo5Pq0AS0+TlfSFPuOUTzru5/Jvmhqvo/29y37dJmzD8F/G5V/cckB4H/0hvzue3v3lRse37N4oz/JLCv7/vLOP+t39+1SXIhzdvD9d5a7XRtxkySfwh8GvhgVb02ob5tl43GfAnwQ8BSkq/S1EKPzvgB3ra/239SVd+uqr8BnqF5IZhVbcb8ceC/AlTVMvDdNNe0mVet/t63YhaD/zHg8iTvSLKL5uDt0YE2R4Ebe9s/Cfx59Y6azKgNx9wre/w2TejPet0XNhhzVb1UVbur6kBVHaA5rvHBqprlD2tu87v9xzQH8kmym6b08+xEezlebcb8deAqgCQ/SBP8pyfay8k6Cnykt7rnR4GXqurUOP+BmSv1VNXZJDcDD9CsCPh8VT2V5JeBlao6CtxJ83bwBM1M/8PT6/HWtRzzfwAuBv5b7zj216vqg1Pr9Ba1HPNcaTnmB4APJHkaeB3451X1zen1emtajvkW4HNJfoGm5PHRWZ7IJfl9mlLd7t5xi18CLgKoqjtojmNcC5wAXgU+NvY+zPD/nyRpE2ax1CNJ2gKDX5I6xuCXpI4x+CWpYwx+SeoYg1+SOsbgl6SOMfglqWP+PxsLCB51UVE2AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#画图\n",
    "#岭系数跟loss值得关系\n",
    "plt.plot(alphas_to_test,model.cv_values_.mean(axis=0),'b.')#mean表示平均值\n",
    "plt.plot(model.alpha_,min(model.cv_values_.mean(axis=0)),'ro')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([90.84710758])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(x_data[3,np.newaxis])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
